/*
 *  Project     : QKeyboardStatistic
 *  Author      : Durachenko Aleksey V. <durachenko.aleksey@gmail.com>, 2011
 *  Licence     : GNU GENERAL PUBLIC LICENSE Version 2
 */
#include "cstatistic.h"

// ----------------------------------------------------------------------
CStatistic::CStatistic(CDatabase* dataBase, int autoSavePeriod, QObject* parent)
    : QObject(parent)
{
    mDatabase = dataBase;
    mAutoSavePeriod = autoSavePeriod;

    // clear counter
    clearLast();

    // current time
    mLastMonth = mLastWeek = mLastDay = mLastHour = QDateTime::currentDateTime();

    // restore statistic from database
    if (mDatabase->isOpen())
    {
        mDatabase->getTotalStatistic(mKeyTotal, mMousePressTotal, mMileageTotal);
        mDatabase->getMonthStatistic(mLastMonth, mKeyMonth, mMousePressMonth, mMileageMonth);
        mDatabase->getWeekStatistic(mLastWeek, mKeyWeek, mMousePressWeek, mMileageWeek);
        mDatabase->getDayStatistic(mLastDay, mKeyDay, mMousePressDay, mMileageDay);
        mDatabase->getHourStatistic(mLastHour, mKeyHour, mMousePressHour, mMileageHour);
    }
    // no database. counter is 0
    else
    {
        mKeyTotal = mKeyMonth = mKeyWeek = mKeyDay = mKeyHour =
        mMousePressTotal = mMousePressMonth = mMousePressWeek =
        mMousePressDay = mMousePressHour = mMileageTotal =
        mMileageMonth = mMileageWeek = mMileageDay = mMileageHour = 0;
    }

    // autosave timer
    mSaveTimer = startTimer(mAutoSavePeriod*60000);

    // start minute timer
    mMinuteTimer = startTimer(60000);
}


// ----------------------------------------------------------------------
CStatistic::~CStatistic()
{
    if (mDatabase->isOpen())
    {
        mDatabase->writeTrio(QDateTime::currentDateTime(), mKeyLast, mMousePressLast, mMileageLast);
        clearLast();
    }
}


// ----------------------------------------------------------------------
void CStatistic::timerEvent(QTimerEvent* event)
{
    if (event->timerId() == mMinuteTimer)
    {
        QDateTime dateTime = QDateTime::currentDateTime();

        if (dateTime.time().hour() != mLastHour.time().hour())
        {
            mLastHour = dateTime;
            mKeyHour = 0;
            mMileageHour = 0;
            mMousePressHour = 0;

            emit keyStatisticChanged();
            emit mousePressStatisticChanged();
            emit mileageStatisticChanged();
        }

        if (dateTime.date().day() != mLastHour.date().day())
        {
            mLastDay = dateTime;
            mKeyDay = 0;
            mMileageDay = 0;
            mMousePressDay = 0;

            emit keyStatisticChanged();
            emit mousePressStatisticChanged();
            emit mileageStatisticChanged();
        }

        if (dateTime.date().weekNumber() != mLastHour.date().weekNumber())
        {
            mLastWeek = dateTime;
            mKeyMonth = 0;
            mMileageMonth = 0;
            mMousePressMonth = 0;

            emit keyStatisticChanged();
            emit mousePressStatisticChanged();
            emit mileageStatisticChanged();
        }

        if (dateTime.date().month() != mLastHour.date().month())
        {
            mLastMonth = dateTime;
            mKeyMonth = 0;
            mMileageMonth = 0;
            mMousePressMonth = 0;

            emit keyStatisticChanged();
            emit mousePressStatisticChanged();
            emit mileageStatisticChanged();
        }
    }
    else if (event->timerId() == mSaveTimer)
    {
        if (mDatabase->isOpen())
        {
            mDatabase->writeTrio(QDateTime::currentDateTime(), mKeyLast, mMousePressLast, mMileageLast);
            clearLast();
        }
    }
}


// ----------------------------------------------------------------------
void CStatistic::incKeyCount()
{
    ++mKeyTotal;
    ++mKeyMonth;
    ++mKeyWeek;
    ++mKeyDay;
    ++mKeyHour;
    ++mKeyLast;

    emit keyStatisticChanged();
}


// ----------------------------------------------------------------------
void CStatistic::incMousePressCount()
{
    ++mMousePressTotal;
    ++mMousePressMonth;
    ++mMousePressWeek;
    ++mMousePressDay;
    ++mMousePressHour;
    ++mMousePressLast;

    emit mousePressStatisticChanged();
}


// ----------------------------------------------------------------------
void CStatistic::incMileage(qint64 mileage)
{
    mMileageTotal += mileage;
    mMileageMonth += mileage;
    mMileageWeek += mileage;
    mMileageDay += mileage;
    mMileageHour += mileage;
    mMileageLast += mileage;

    emit mileageStatisticChanged();
}


// ----------------------------------------------------------------------
void CStatistic::clearLast()
{
    mKeyLast = 0;
    mMousePressLast = 0;
    mMileageLast = 0;
}
